package mr.go.yaxc.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.net.URL;
import java.util.Map;

import mr.go.yaxc.ActionMatch;
import mr.go.yaxc.Actions;
import mr.go.yaxc.XmlConfigurator;
import mr.go.yaxc.actions.SetObjectPropertyInfo;
import mr.go.yaxc.tests.test2.Something;
import mr.go.yaxc.tests.test2.SomethingHolder;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class Test2 {

    @BeforeClass
    public static void setUpBeforeClass()
            throws Exception {
        xmlUrl = Test2.class.getClassLoader().getResource("test2.xml");
        if (xmlUrl == null) {
            throw new IllegalStateException("No xml file");
        }
        final SetObjectPropertyInfo actionInfo = new SetObjectPropertyInfo(
                ActionMatch.LEVEL,
                "",
                200);
        actionInfo.setLevel(2);
        Actions.registerAction(actionInfo);
    }

    @AfterClass
    public static void tearDownAfterClass()
            throws Exception {
        Actions.resetConfiguration();
    }
    private XmlConfigurator configurator1;
    private SomethingHolder somethingHolder1;

    @Before
    public void setUp()
            throws Exception {
        configurator1 = new XmlConfigurator();
        somethingHolder1 = new SomethingHolder();
        configurator1.setExternalReference(
                "externalSomethingHolder",
                somethingHolder1);
        configurator1.parse(xmlUrl);
    }

    @Test
    public void testArrayCreationAndStaticMethodCall()
            throws Exception {
        Something[] somethings = somethingHolder1.getSomethings();
        assertEquals(4,
                     somethings.length);
    }

    @Test
    public void testCreate()
            throws Exception {
        Something sth = somethingHolder1.getSpecialSomething();
        assertEquals("a",
                     sth.getString());
        assertEquals(2,
                     sth.getInteger());
    }

    @Test
    public void testMap()
            throws Exception {
        Map<String, Integer> names = somethingHolder1.getNames();
        assertEquals(0,
                     names.get("name1").intValue());
        assertEquals(1,
                     names.get("name2").intValue());
    }

    @Test
    public void testSetProperty()
            throws Exception {
        Something sth = somethingHolder1.getOrdinarySomething();
        assertTrue(sth.isValue());
    }
    private static URL xmlUrl;
}
